package com.v1.video.activity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.v1.video.Constant;
import com.v1.video.R;
import com.v1.video.domain.Channel;
import com.v1.video.domain.CustomChannelLst;
import com.v1.video.util.Utility;

import java.io.Serializable;
import java.util.List;
/**
 * 频道管理
 * @author wangfang
 *
 */
public class ChannelManagerActivity2 extends BaseActivity {

	private List<Channel> mChannelLst;
	private boolean isMove = false;
	private Handler mHandler = new Handler();
	private ImageView iv_back;
	private GridView gridView;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_channelmanager2);

	}

	@Override
	protected void initView() {
		iv_back = (ImageView) findViewById(R.id.iv_back);
		gridView = (GridView) findViewById(R.id.gridView);
	}

	@Override
	protected void initData() {
		// TODO Auto-generated method stub
		Intent intent = getIntent();
		int index = intent.getIntExtra("Index", 0);
		loadCachedData();
		GridAdapter  adapter = new GridAdapter();
		gridView.setAdapter(adapter);
	}

	@Override
	protected void setListener() {
		// TODO Auto-generated method stub
		findViewById(R.id.ll_back).setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				finish();
			}
		});
			
	}

	private void loadCachedData(){
		String json = Utility.ReadTxtFile(this,
				"CustomChannelCache.txt");
		if (!TextUtils.isEmpty(json)) {
			Gson gson = new Gson();
			CustomChannelLst mCustomChannelLst = gson.fromJson(json,CustomChannelLst.class);
			mChannelLst = mCustomChannelLst.getUserLst();
		}
	}
	
	private class GridAdapter extends BaseAdapter{

		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return mChannelLst.size();
		}

		@Override
		public Object getItem(int position) {
			// TODO Auto-generated method stub
			return null;
		}

		@Override
		public long getItemId(int position) {
			// TODO Auto-generated method stub
			return 0;
		}

		@Override
		public View getView(final int position, View convertView, ViewGroup parent) {
			ViewHolder holder;
			if(convertView == null){
				convertView = View.inflate(ChannelManagerActivity2.this, R.layout.item_channelmanger_grid, null);
				holder = new ViewHolder();
				holder.iv_img = (ImageView) convertView.findViewById(R.id.iv_img);
				holder.tv_channel = (TextView) convertView.findViewById(R.id.tv_channel);
				convertView.setTag(holder);
			}else{
				holder = (ViewHolder) convertView.getTag();
			}
			if(mChannelLst != null && mChannelLst.size()>0){
				Channel channel = mChannelLst.get(position);
				if (!TextUtils.isEmpty(channel.getImg())) {
					ImageLoader.getInstance().displayImage(channel.getImg(),
							holder.iv_img,
							Constant.VIDEO_9_16_OPTION_2);
				} else {
					holder.iv_img
							.setImageResource(R.drawable.common_place_holder_bg_16_10);
				}
				if (!TextUtils.isEmpty(channel.getName())) {
					holder.tv_channel.setText(channel.getName());
				}else{
					holder.tv_channel.setText("");
				}
				
			}
			convertView.setOnClickListener(new OnClickListener() {
				
				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub
					Intent intent = new Intent();
					intent.putExtra("userChannel", (Serializable) mChannelLst);
					intent.putExtra("Index", position);
					setResult(RESULT_OK, intent);
					finish();
				}
			});
			// TODO Auto-generated method stub
			return convertView;
		}

		
	}
	
	class ViewHolder {
		ImageView iv_img;
		TextView tv_channel;
	}


}
